package com.lz.base.project.thread.completableFuture;

import cn.hutool.core.date.StopWatch;
import com.lz.base.project.thread.executor.ExecutorConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 *
 */
public class CompletableFutureLz {

    public static void main(String[] args) {

        t2();
    }

    public static void t1() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService service = Executors.newFixedThreadPool(5);

        List<String> cityList = new ArrayList<>();
        cityList.add("上海");
        cityList.add("北京");
        cityList.add("广州");
        cityList.add("深圳");
        cityList.add("杭州");
        for (int i = 0; i < 100; i++) {
            cityList.add("上海");
            cityList.add("北京");
            cityList.add("广州");
            cityList.add("深圳");
            cityList.add("杭州");
        }
        for (int i = 0; i < cityList.size(); i++) {
            String city = cityList.get(i);
            CompletableFuture<Object> completableFuture
                    = CompletableFuture.supplyAsync(() -> {
                try {
                    return Weather.getWeather(city);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, service);
        }

        System.out.println("CompletableFutureLz结束");
        stopWatch.stop();
        System.out.println("CompletableFutureLz总耗时：" + stopWatch.getTotalTimeMillis());
    }

    public static void t2() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService service = Executors.newFixedThreadPool(10);

        List<String> cityList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            cityList.add("1000");
            cityList.add("12");
            cityList.add("900000000");
            cityList.add("1");
            cityList.add("798000");
        }
        CountDownLatch countDownLatch = new CountDownLatch(5000);
        for (String city : cityList) {
            CompletableFuture<Object> completableFuture
                    = CompletableFuture.supplyAsync(() -> {
                try {
                    return WeatherLatch.getWeather(city, countDownLatch);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, ExecutorConfiguration.executors);
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("CompletableFutureLz2结束");
        stopWatch.stop();
        System.out.println("CompletableFutureLz2总耗时：" + stopWatch.getTotalTimeMillis());
    }

    public static void t3() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ExecutorService service = Executors.newFixedThreadPool(5);

        List<String> cityList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            cityList.add("上海");
            cityList.add("北京");
            cityList.add("广州");
            cityList.add("深圳");
            cityList.add("杭州");
        }
        for (int i = 0; i < cityList.size(); i++) {
            String city = cityList.get(i);
            try {
                Weather.getWeather(city);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println("CompletableFutureLz3结束");
        stopWatch.stop();
        System.out.println("CompletableFutureLz3总耗时：" + stopWatch.getTotalTimeMillis());
    }
}
